package com.hzhr.iot.collect.protocol.processor;

import com.hzhr.iot.collect.Global;
import com.hzhr.iot.collect.constant.MessageFieldKeyEnum;
import com.hzhr.iot.collect.protocol.util.MessageUtil;
import com.hzhr.iot.collect.service.helper.Device_CollectDeviceServiceHelper;
import com.hzhr.iot.collect.service.helper.Device_IotDeviceServiceHelper;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.sdk.model.collectDevice_valveStatus.CollectDevice_ValveStatusPutModel;
import com.hzhr.iot.device.sdk.model.iotDevice.IotDeviceModel;
import com.hzhr.iot.device.sdk.service.Device_CollectDevice_ValveStatusService;
import com.hzhr.iot.device.sdk.service.Device_IotDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 集中器 阀门状态查询
 * @author 48310
 *
 */
@Component
public class MessageProcessor85 implements MessageProcessor {
	@Autowired
	MessageUtil messageUtil;
	@Autowired
	Device_IotDeviceService device_iotDeviceService;
	@Autowired
	Device_IotDeviceServiceHelper device_iotDeviceServiceHelper;
	@Autowired
	Device_CollectDevice_ValveStatusService device_collectDevice_valveStatusService;
	@Autowired
	Device_CollectDeviceServiceHelper device_collectDeviceServiceHelper;
	@Autowired
	Global global;

	@Override
	public List<Map<MessageFieldKeyEnum, Object>> proccess(int protocolId, String deviceId, Map<MessageFieldKeyEnum, Object> parsedMessageObj) throws Exception {
		String ccrCode = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.RESP_CONCENTRATOR_NO));
		String meterCode = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.RESP_METER_NO));
		Integer valveStatus = HzhrTypeUtil.object2integer(parsedMessageObj.get(MessageFieldKeyEnum.RESP_VALVE_STATUS));

		if (!StringUtils.isEmpty(ccrCode) && !StringUtils.isEmpty(meterCode) && valveStatus != null) {
			IotDeviceModel ccr = device_iotDeviceServiceHelper.getByCode(ccrCode);
			if (ccr != null) {
				Integer meterId = device_collectDeviceServiceHelper.getIdByIotDeviceIdAndCode(ccr.getId(), meterCode);

				if (meterId != null) {
					CollectDevice_ValveStatusPutModel body = new CollectDevice_ValveStatusPutModel();
					if (valveStatus == 0) {
						body.setCurrentValveStatusCode("2");
					}
					if (valveStatus == 1) {
						body.setCurrentValveStatusCode("1");
					}
					if (valveStatus == 2) {
						body.setCurrentValveStatusCode("3");
					}
					Map<String, Object> query = new HashMap<>();
					device_collectDevice_valveStatusService.putByCollectDeviceId(global.initGlobalHeader(), meterId, query, body);
				}
			}
		}

		return null;
	}
}
